<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body></body>
<script>
    // 队列：先进先出原则（排队），一组有序的项
    // 创建队列类
    class Queue {
        #count = 0; // 控制队列的长度
        #lowestCount = 0; // 控制追踪第一个元素，常量为 0
        constructor() {
            this.items = {};
        }
        // 添加到队列里面
        enqueue = (ele) => {
            this.items[this.#count] = ele;
            this.#count++;
        };
        // 清空队列
        clear = () => {
            this.#count = 0;
            this.items = {};
        };
        // 队列移除元素
        dequeue = () => {
            if (this.#count === 0) {
                return null;
            }
            const result = this.items[this.#lowestCount];
            delete this.items[this.#lowestCount];
            let temp = {};
            Object.entries(this.items).forEach((it, i) => {
                it[0] = i;
                temp[it[0]] = it[1];
            });
            this.items = temp;
            temp = null;
            this.#count--;
            return result;
        };
        // 检查队列是否为空
        isEmpty = () => {
            return this.#count === 0;
        };
        // 查看队列长度
        size = () => {
            return this.#count;
        };
        // 查看队列头元素
        peek = () => {
            if (this.isEmpty()) return null;
            return this.items[this.#lowestCount];
        };
        // 创建 toString 方法
        toString = () => {
            if (this.isEmpty()) {
                return "{}";
            }
            let str = "{";
            for (let i = 0; i < this.#count; i++) {
                str += `${i}: ${this.items[i]}, `;
            }
            str = str.slice(0, str.length - 2);
            return str + "}";
        };
    }
    let y = new Queue();
    y.enqueue("ee");
    y.enqueue("99");
    y.enqueue("939");
    y.enqueue("949");
    y.dequeue();
    let uu = y.dequeue();
    y.enqueue("ee");

    // 双端队列：是一种允许我们同时从前端和后端添加和移除元素的特殊队列，同时遵守了先进先出和后进先出的原则，相当于队列和栈的组合
    // 创建双端队列
    class Deque {
        #count = 0; // 控制、记录队列长度
        constructor() {
            this.items = {};
        }
        // 清空队列
        clear = () => {
            this.#count = 0;
            this.items = {};
        };
        // 检查队列是否为空
        isEmpty = () => {
            return this.#count === 0;
        };
        // 队列长度
        size = () => {
            return this.#count;
        }
        // 后端添加元素
        addBack = (ele) => {
            this.items[this.#count] = ele
            this.#count++
        }
        // 前端添加
        addFront = (ele) => {
            if (this.isEmpty()) {
                this.addBack(ele)
            } else {
                Object.entries(this.items).forEach((it, i) => {
                    this.items[i + 1] = it[1]
                })
                this.items[0] = ele
            }
            this.#count++
        }
        // 后端移除，返回被移除元素
        removeBack = () => {
            if (this.isEmpty()) {
                return null
            }
            const lastElement = this.peekBack()
            delete this.items[Object.entries(this.items).length - 1]
            this.#count--
            return lastElement
        }
        // 前端移除，返回被移除元素
        removeFront = () => {
            if (this.isEmpty()) {
                return null
            }
            Object.entries(this.items).forEach((it, i) => {
                this.items[i - 1] = it[1]
            })
            const firstElement = this.items[-1]
            delete this.items[-1]
            this.removeBack(this.items)
            return firstElement
        }
        // 返回第一个元素
        peekFront = () => {
            if (this.isEmpty()) {
                return null
            }
            return this.items[0]
        }
        // 返回末端元素
        peekBack = () => {
            if (this.isEmpty()) {
                return null
            }
            return this.items[Object.entries(this.items).length - 1]
        }
        // 创建 toString 方法
        toString = () => {
            if (this.isEmpty()) {
                return "{}";
            }
            let str = "{";
            for (let i = 0; i < this.#count; i++) {
                str += `${i}: ${this.items[i]}, `;
            }
            str = str.slice(0, str.length - 2);
            return str + "}";
        };
    }
    let yd = new Deque();
    yd.addBack("000")
    yd.addBack("111")
    yd.addFront("-111")
    yd.removeBack()
    yd.addFront("-222")
    console.log(yd.toString());
</script>

</html>